package com.itheima.sh.controller;

import com.itheima.sh.clients.OrderFeignApi;
import com.itheima.sh.clients.UserFeignApi;
import com.itheima.sh.pojo.Order;
import com.itheima.sh.pojo.Result;
import com.itheima.sh.pojo.User;
import com.itheima.sh.vo.UserOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("feign")
public class FeignController {

    @Autowired
    UserFeignApi userFeignApi;//jdk

    @Autowired
    OrderFeignApi orderFeignApi;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable("id")String id){
        return userFeignApi.queryById(id);
    }

    @GetMapping("/userOrder/{username}")
    public Result findUserById(@PathVariable("username")String username){
        List<Order> orderList = orderFeignApi.findOrderByUserId(username);
        User user = userFeignApi.findUserById(username);
        UserOrderVO userOrderVO = new UserOrderVO();
        userOrderVO.setUsername(user.getUsername());
        userOrderVO.setPhone(user.getPhone());
        userOrderVO.setNickName(user.getNickName());
        userOrderVO.setHeadPic(user.getHeadPic());
        userOrderVO.setOrderList(orderList);
        return new Result(userOrderVO);
    }
}
